package aufgabe01;

import java.util.ArrayList;

public class BinTree
{
  private Node root;
  
  public BinTree(int startValue)
  {
    root = new Node();
    root.value = startValue;
  }
  
  class Node
  {
    int value;
    Node leftChild;
    Node rightChild;
    Node parent;
    
    public String toString()
    {
      return value+"";
    }
  }
  
  public void insert(int value)
  {
    Node newNode = new Node();
    newNode.value = value;
    Node thisNode = root;
    while(thisNode.leftChild != null || thisNode.rightChild != null)
    {
      if(value < thisNode.value && thisNode.leftChild!=null)
        thisNode = thisNode.leftChild;
      else if(value > thisNode.value && thisNode.rightChild!=null)
        thisNode = thisNode.rightChild;
    }
    newNode.parent = thisNode;
    
    if(newNode.value<newNode.parent.value)
      newNode.parent.leftChild = newNode;
    else
      newNode.parent.rightChild = newNode;
  }
  
  public void remove(int value)
  {
    
  }
  
  public String toString()
  {
    String output = "";
    
    // Erstes Level
    output += root+"\n";
      
    // Zweites Level
    output += root.leftChild;
    output += root.rightChild+"\n";
    
    // Drittes Level
    output += root.leftChild.leftChild;
    output += root.leftChild.rightChild+"\n";
    
    ArrayList<Integer> allValues = new ArrayList<Integer>();
    
    Node thisNode = root;
    int level = 0;
    while(thisNode.leftChild != null)
    {
      allValues.add(thisNode.value);
      thisNode = thisNode.leftChild;
      level++;
      System.out.println("ThisNode ist im Level "+level);
    }
    allValues.add(thisNode.value);
    while(thisNode.rightChild == null || allValues.contains(thisNode.rightChild.value))
    {
      thisNode = thisNode.parent;
      if(!allValues.contains(thisNode.value))
        allValues.add(thisNode.value);
      if(thisNode.equals(root))
        break;
      level--;
      System.out.println("ThisNode ist im Level "+level);
    }
    thisNode = thisNode.rightChild;
    
    for(int value : allValues)
      System.out.println(value);
    
    
      
    return "";
  }
}
